#include "plotbitmap.h"

PlotBitmap::PlotBitmap(QObject *parent) : QObject(parent)
{

}

//QBitmap PlotBitmap::Create1BPPBitmap(int bTransparent, int crBkColor, int iWidth, int iHeight, int iStride, int nPixFormat, char *pBmpData)
QBitmap PlotBitmap::Create1BPPBitmap(int iWidth,int iHeight,unsigned  char *pBmpData)
{
    QPen pen;
    pen.setWidth(1);
    pen.setColor(QColor(Qt::black));

    //QImage::Format_RGBA8888 代表彩色图
    QImage image = QImage(pBmpData, iWidth, iHeight, QImage::Format_RGB888);
    //image.save("D:\\1.png");

    QBitmap pixmap = QBitmap(iWidth,iHeight);
    QPainter *pixPainter = new QPainter();
    pixPainter->setPen(pen);
    pixPainter->begin(&pixmap);
    pixmap.fill(Qt::white);//用白色填充
    pixPainter->drawImage(0,0,image);
    //pixmap.save("D:\\1.bmp");

#if(0)//转灰度图
     QImage gimage = QImage();
     uchar* pImageData1 = NULL,*pImageData2 = NULL;

     pImageData1 = image.bits();

     gimage = image.convertToFormat(QImage::Format_Indexed8);

     QVector<QRgb> table;
     for( int i = 0 ; i < 256 ; ++i )
     {
     table.append(qRgb(i,i,i));
     }

     gimage.setColorTable(table);

     for(int i =0; i< gimage.width();i++)
     {
     for(int j=0; j< gimage.height();j++)
     {
     QRgb pix_value = gimage.pixel(i,j);
     gimage.setPixel(i,j,qGray(pix_value));
     }

     }

     pImageData2 = gimage.bits();
     gimage.save("D:\\1.bmp");

#endif
    return pixmap;
}

//设置unsigned char中某位的值
//dat 需要设置的unsigned char
//index 设置位
//val 设置值0或者1
void PlotBitmap::SetUncharBit(unsigned char &dat, int index, int val)
{
    unsigned int bitmod = 0x01;
    if(index >= 0 && index < 8)
    {
        bitmod <<= index;
        if (val == 0)
        {
            dat &= ~bitmod;
        }
        else
        {
            dat |= bitmod;
        }
    }
}
